package com.wan.net.rpc.invoker.balancer;

import com.wan.net.rpc.service.RpcService;

import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 随机选取
 * @author wwh
 * @date 2022/6/22 16:14
 */
public class RandomBalancer implements Balancer<RpcService>{
    private final Random random;

    public RandomBalancer() {
        this.random = ThreadLocalRandom.current();
    }

    @Override
    public RpcService choose(List<RpcService> list) {
        if(list == null || list.isEmpty()) return null;
        int index = random.nextInt(list.size());
        return list.get(index);
    }
}
